package com.vietnam.power.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.vietnam.power.constant.AppConfigurationConstant;
import com.vietnam.power.domain.MessageInfo;
import com.vietnam.power.domain.MessageSearchCriteria;
import com.vietnam.power.service.MessageService;

@Controller
@RequestMapping("/admin")
public class MailController {
	private static Logger logger = Logger.getLogger(MailController.class);
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private MessageService messageService;

	@RequestMapping(value = { "/getMailBox" }, method = RequestMethod.GET)
	public ModelAndView getMailBox(@RequestParam(value = "mode", required = true) String mode) {
		logger.debug("MODE : " + mode);
		ModelAndView mav = new ModelAndView("admin/mailbox");

		MessageSearchCriteria s = new MessageSearchCriteria();
		if (StringUtils.equalsIgnoreCase(mode, AppConfigurationConstant.MESSAGE.INBOX)) {
			s.setSection(AppConfigurationConstant.MESSAGE.INBOX);
		} else if (StringUtils.equalsIgnoreCase(mode, AppConfigurationConstant.MESSAGE.SEND)) {
			s.setSection(AppConfigurationConstant.MESSAGE.SEND);
		} else if (StringUtils.equalsIgnoreCase(mode, AppConfigurationConstant.MESSAGE.TRASH)) {
			s.setSection(AppConfigurationConstant.MESSAGE.TRASH);
		} else {
			throw new RuntimeException("Message mode is wrong.");
		}

		mav.addObject("messages", messageService.searchMessages(s));
		mav.addObject("pageTitle", messageSource.getMessage("admin.mailbox.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/checkEmailAlert" }, method = RequestMethod.POST)
	public @ResponseBody
	List<MessageInfo> checkEmailAlert() {
		MessageSearchCriteria s = new MessageSearchCriteria();
		s.setStatus(Boolean.FALSE);
		s.setSection(AppConfigurationConstant.MESSAGE.INBOX);
		return messageService.searchMessages(s);
	}

	@RequestMapping(value = { "/readEmail" }, method = RequestMethod.GET)
	public ModelAndView readEmail(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = false) int id) {
		ModelAndView mav = new ModelAndView("admin/readEmail");

		MessageInfo message = messageService.getMessageById(id);
		mav.addObject("message", message);
		messageService.markMessageAsRead(id);

		mav.addObject("pageTitle", messageSource.getMessage("admin.mailbox.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/markMessageAsRead" }, method = RequestMethod.POST)
	public @ResponseBody
	String markMessageAsRead(@RequestParam(value = "ids", required = false) String jsonString) {
		Gson gson = new Gson();
		int[] ids = gson.fromJson(jsonString, int[].class);
		for (int id : ids) {
			messageService.markMessageAsRead(id);
		}

		return "success";
	}

	@RequestMapping(value = { "/markMessageAsUnRead" }, method = RequestMethod.POST)
	public @ResponseBody
	String markMessageAsUnRead(@RequestParam(value = "ids", required = false) String jsonString) {
		Gson gson = new Gson();
		int[] ids = gson.fromJson(jsonString, int[].class);
		for (int id : ids) {
			messageService.markMessageAsUnRead(id);
		}

		return "success";
	}

	@RequestMapping(value = { "/deleteMessages" }, method = RequestMethod.POST)
	public @ResponseBody
	String deleteMessages(@RequestParam(value = "ids", required = false) String jsonString,
			@RequestParam(value = "mode", required = false) String mode) {
		Gson gson = new Gson();
		int[] ids = gson.fromJson(jsonString, int[].class);

		if (StringUtils.equalsIgnoreCase(mode, AppConfigurationConstant.MESSAGE.TRASH)) {
			for (int id : ids) {
				messageService.deleteMessages(id);
			}
		} else {
			for (int id : ids) {
				messageService.moveToTrash(id);
			}
		}

		return "success";
	}

	@RequestMapping(value = { "/createEmail" }, method = RequestMethod.POST)
	public @ResponseBody
	String createEmail(@RequestBody MessageInfo mess) {
		messageService.createEmail(mess);
		return "success";
	}
}
